package com.sunshine.wrongquestionset.activity.exercise_history

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView
import com.google.android.material.textview.MaterialTextView
import com.sunshine.wrongquestionset.R
import com.sunshine.wrongquestionset.activity.main.SearchBean
import com.sunshine.wrongquestionset.room.entity.WrongQuestionBean
import com.sunshine.wrongquestionset.room.entity.WrongQuestionEntity
import com.sunshine.wrongquestionset.view.HasEmptyRecyclerAdapter
import com.sunshine.wrongquestionset.view.QuestionContentBottomSheetDialog
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author sunshine
 * @date 2021/4/13
 */
class ExerciseHistoryRecyclerAdapter(private var list: List<WrongQuestionBean>?) :
    HasEmptyRecyclerAdapter<ExerciseHistoryRecyclerAdapter.ViewHolder>(list) {

    private lateinit var context: Context
    @SuppressLint("SimpleDateFormat")
    private val simpleDateFormat = SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss")

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mRoot: MaterialCardView = itemView.findViewById(R.id.root_wrong_question)
        val questionName: MaterialTextView = itemView.findViewById(R.id.question_name)
        val mDescription: MaterialTextView = itemView.findViewById(R.id.description)
        val mScore: MaterialTextView = itemView.findViewById(R.id.score)
        val imageScore: ImageView = itemView.findViewById(R.id.imageView_score)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_wrong_question, parent, false))
    }

    override fun getItemCount(): Int {
        return list?.size ?: 0
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mRoot.setOnClickListener {
            QuestionContentBottomSheetDialog.showWrongQuestion(
                context,
                list!![position].titlePath,
                list!![position].reallyAnswerPath,
                list!![position].answerPath,
                "${list!![position].score}/${list!![position].totalScore}分"
            )
        }
        holder.questionName.text =
            if (list!![position].titlePath.contains(Regex("!\\[.*]\\(.*\\)"))) "内含图片，请点击查看"
            else list!![position].titlePath
        holder.mDescription.text = simpleDateFormat.format(Date(list!![position].date))
        holder.mScore.text = "${list!![position].score}/${list!![position].totalScore}分"

        when ((list!![position].score) / (list!![position].totalScore)) {
            in 0.0f..0.33f -> {
                holder.imageScore.setImageResource(R.drawable.ic_sad)
            }
            in 0.33f..0.66f -> {
                holder.imageScore.setImageResource(R.drawable.ic_smile_small)
            }
            else-> {
                holder.imageScore.setImageResource(R.drawable.ic_smile_big)
            }
        }
    }

    fun setFilter(list: List<WrongQuestionBean>?) {
        this.list = list
        notifyDataSetChanged(list)
    }
}